//app.js
import GlobalConfig from './config/index'
import { http } from './common/api/index.js'
import { IMG_BASE_URL, ERR_OK } from './common/config.js'
import { message, router } from './common/utils/index.js'
global.request = http;
global.token = ""
global.openid = ""
global.userInfo = {
  nickName: ''
}
global.message = message
global.router = router
global.logo = {}
global.qrCode = {}
global.name = ''
global.c_id = ''
const globalConfig = new GlobalConfig()
// 获取账户信息
try {
  const PET_COM_ID = wx.getStorageSync('PET_COM_ID');
  const PET_TOKEN = wx.getStorageSync('PET_TOKEN');
  const PET_OPEN_ID = wx.getStorageSync('PET_OPEN_ID');
  const USER_INFO = wx.getStorageSync('USER_INFO');
  global.c_id = PET_COM_ID || '';
  global.token = PET_TOKEN || '';
  global.openId = PET_OPEN_ID || '';
  if (USER_INFO) global.userInfo = USER_INFO;
} catch (e) {
  console.log(e)
}

// 页面跳转封装
global.handleJump = function (e) {
  let target = e.currentTarget || e.target,
    url = target.dataset.url || 'back',
    type = target.dataset.type;
  if (url == '' || url == '#') return;
  switch (type) {
    case 'SWITCH':
      wx.switchTab({ url });
      break;
    case 'REDIRECT':
      wx.redirectTo({ url });
      break;
    case 'RELAUNCH':
      wx.reLaunch({ url });
      break;
    case 'BACK':
      wx.navigateBack();
      break;
    default:
      wx.navigateTo({ url });
      break;
  }
}

App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        console.log('code is', res.code)
        http.post('/api/wx/xiaochengxu/login', {
          code: res.code
        }).then((res) => {
          const data = res.data
          if (data.code === ERR_OK) {
            global.userInfo = {
              nickName: data.rows[0].nickName,
              pic: data.rows[0].pic,
              sex: data.rows[0].sex
            };
            global.token = data.rows[0].token;
            global.openid = data.rows[0].openId;
            wx.setStorageSync('USER_INFO', global.userInfo);
            wx.setStorageSync('PET_TOKEN', global.token);
            wx.setStorageSync('PET_OPEN_ID', global.openid);
          }
          console.log(res)
        }).catch((err) => {
          console.log(err)
        })
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  assembleImgSrc: (input) => {
    return input.indexOf('http://') != -1 || input.indexOf('https://') != -1 ? input : IMG_BASE_URL + input
  },
  globalData: {
    config: globalConfig,
    userInfo: null
  }
})